package com.bd.scm.module.jurisdiction.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bd.scm.config.MyLog;
import com.bd.scm.domain.dto.jurisdiction.BdSysLoginUserDto;
import com.bd.scm.domain.models.jurisdiction.BdSysLoginUser;
import com.bd.scm.domain.models.jurisdiction.BdSysRole;
import com.bd.scm.module.jurisdiction.service.BdSysLoginUserService;
import com.bd.scm.module.jurisdiction.service.BdSysRoleService;
import com.bd.scm.module.utils.ResultUtil;
import com.bd.utils.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/** 登陆人逻辑
 * @author YH
 * @version 1.0
 * @date 2020/12/15 9:54
 */
@RestController
@RequestMapping("api/bdSysLoginUser")
public class BdSysLoginUserController {

    @Autowired
    private BdSysLoginUserService bdSysLoginUserService;

    @Autowired
    private BdSysRoleService bdSysRoleService;

    /**
     * 获取登陆人列表
     *
     * @return
     */
    @PostMapping("getUserList")
    public Result getUserList(@RequestBody BdSysLoginUserDto bdSysLoginUserDto, HttpServletRequest request) {
        Result result = null;
        Page<BdSysLoginUser> page = new Page<>(bdSysLoginUserDto.getCurrent(),
                bdSysLoginUserDto.getPageSize());
        IPage<BdSysLoginUser> pageList = this.bdSysLoginUserService.getUserList(page, bdSysLoginUserDto);
        result = ResultUtil.ok(pageList);
        return result;
    }

    /**
     * 获取权限集合
     * @return
     */
    @PostMapping("getRoleList")
    public Result getRoleList(){
        Result result = null;
        List<BdSysRole> roleList = this.bdSysRoleService.getRoleList();
        result = ResultUtil.ok(roleList);
        return result;
    }

    /**
     * 用户添加 修改
     * @param bdSysLoginUser
     * @return
     */
    @MyLog(value = "管理员定义-用户添加 修改")
    @PostMapping("savaUser")
    public Result savaUser(@RequestBody BdSysLoginUser bdSysLoginUser,HttpServletRequest request){
        Result result = null;
        //获取登录人ID
        JSONObject user = null;
        try {
            //添加操作人ID
            user = (JSONObject) request.getSession().getAttribute("user");
            try {
                //添加
                if (bdSysLoginUser.getId() <= 0) {
                    bdSysLoginUser.setCreationUser(user.getString("userSerial"));
                    result = this.bdSysLoginUserService.addUser(bdSysLoginUser);
                } else {
                    //修改
                    bdSysLoginUser.setUpdateUser(user.getString("userSerial"));
                    result = this.bdSysLoginUserService.updateUser(bdSysLoginUser);
                }
            } catch (Exception e) {
                e.printStackTrace();
                result = Result.error("服务器内部错误，保存失败");
            }
        }catch (NullPointerException n) {
            n.printStackTrace();
            result = Result.error("登录信息失效，请重新登录");
        }
        return result;
    }

    /**
     * 删除
     * @param bdSysLoginUser
     * @return
     */
    @MyLog(value = "管理员定义-用户删除")
    @PostMapping("deleteUser")
    public Result deleteUser(@RequestBody BdSysLoginUser bdSysLoginUser){
        Result result = null;
        try {
            result = this.bdSysLoginUserService.deleteUser(bdSysLoginUser.getId());
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.error("服务器内部错误，删除失败");
        }
        return result;
    }
}
